package org.softmed.rest.server.xstream;

import java.util.HashMap;
import java.util.Map;
import org.softmed.persistence.PersistenceManager;
import org.softmed.rest.config.Application;
import org.softmed.rest.generation.xml.XMLMarshaller;

public class GenericToXML implements XMLMarshaller {
	static Map<Class, XMLMarshaller> converters = new HashMap<Class, XMLMarshaller>();
	Map<Class, XMLMarshaller> myConverters = new HashMap<Class, XMLMarshaller>();
	XMLMarshaller converter;

	public GenericToXML() {
		myConverters.putAll(converters);
	}

	// public GenericToXML(Object obj) {
	// // chooseConverter(obj, null, null, null);
	// }

	public String toXML(Object obj, PersistenceManager util, Application app,
			Object... options) throws Throwable {
		if (converter == null) {
			chooseConverter(obj, util, app, options);
		}
		return converter.toXML(obj, util, app, options);
	}

	public GenericToXML clone() {
		return new GenericToXML();
	}

	private void chooseConverter(Object obj, PersistenceManager util,
			Application app, Object... options) throws Throwable {
		converter = converters.get(obj.getClass());
		if (converter == null) {
			// converter = new DefaultToXMLHideNonNativeConverter();
			converter = XMLCache.getNormalConverter(obj.getClass(), util, app,
					options);
		}
		// } else
		// converter = converter.clone();

	}

	public static Map<Class, XMLMarshaller> getConverters() {
		return converters;
	}

}
